package com.jyniubi.gmail.pms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyniubi.gmail.common.bean.PageResultVo;
import com.jyniubi.gmail.common.bean.PageParamVo;

import com.jyniubi.gmail.pms.mapper.CategoryMapper;
import com.jyniubi.gmail.pms.entity.CategoryEntity;
import com.jyniubi.gmail.pms.service.CategoryService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> queryCategoriesById(Long parentId) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        if (parentId != -1){
            wrapper.eq("parent_id",parentId);
        }
        return this.list(wrapper);
    }

    @Override
    public List<CategoryEntity> queryLvl23CategoriesWithSubs(Long pid) {
        List<CategoryEntity> categoryEntities = this.categoryMapper.queryCategoriesByPid(pid);
        return categoryEntities;
    }

    @Override
    public List<CategoryEntity> queryLvl123CategoriesByCid3(Long cid3) {
        // 根据三级分类的id查询出三级分类
        CategoryEntity categoryEntity3 = this.getById(cid3);
        if (categoryEntity3 == null){
            return null;
        }
        // 根据三级分类的父id查询出二级分类
        CategoryEntity categoryEntity2 = this.getById(categoryEntity3.getParentId());
        // 根据二级分类的父id查询出一级分类
        CategoryEntity categoryEntity1 = this.getById(categoryEntity2.getParentId());

        return Arrays.asList(categoryEntity1,categoryEntity2,categoryEntity3);
    }

}